/*
 * EBKlantenBestand.java
 *
 * Created on May 30, 2008, 8:51 PM
 */

package energiebedrijf;

import energiebedrijf.Classes.Klant;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 * JDialog klasse om een klanten bestand op te slaan in een bestand en weer 
 * terug te lezen.
 * @author  pdb
 */
public class EBKlantenBestand extends javax.swing.JDialog {
    private List _klanten;
    
    /** 
     * Met deze contructor wordt een nieuw formulier gemaakt en wordt de huidige
     * lijst met klanten doorgegeven.
     */
    public EBKlantenBestand(java.awt.Frame parent, boolean modal, List klanten) {
        super(parent, modal);
        initComponents();
        this._klanten = klanten;
        
        this.setLocationRelativeTo(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSluiten = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSaveBestand = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();
        btnSaveKlanten = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtOpenFile = new javax.swing.JTextField();
        btnBrowseOpen = new javax.swing.JButton();
        btnKlantenLezen = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtResultaat = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getResourceMap(EBKlantenBestand.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getActionMap(EBKlantenBestand.class, this);
        btnSluiten.setAction(actionMap.get("btnSluiten_Click")); // NOI18N
        btnSluiten.setText(resourceMap.getString("btnSluiten.text")); // NOI18N
        btnSluiten.setName("btnSluiten"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtSaveBestand.setEditable(false);
        txtSaveBestand.setText(resourceMap.getString("txtSaveBestand.text")); // NOI18N
        txtSaveBestand.setName("txtSaveBestand"); // NOI18N

        btnBrowse.setAction(actionMap.get("btnBrowse_Click")); // NOI18N
        btnBrowse.setText(resourceMap.getString("btnBrowse.text")); // NOI18N
        btnBrowse.setName("btnBrowse"); // NOI18N

        btnSaveKlanten.setAction(actionMap.get("btnSaveKlanten_Click")); // NOI18N
        btnSaveKlanten.setText(resourceMap.getString("btnSaveKlanten.text")); // NOI18N
        btnSaveKlanten.setEnabled(false);
        btnSaveKlanten.setName("btnSaveKlanten"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtSaveBestand, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBrowse))
                    .addComponent(btnSaveKlanten, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(btnBrowse)
                    .addComponent(txtSaveBestand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSaveKlanten)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtOpenFile.setEditable(false);
        txtOpenFile.setText(resourceMap.getString("txtOpenFile.text")); // NOI18N
        txtOpenFile.setName("txtOpenFile"); // NOI18N

        btnBrowseOpen.setAction(actionMap.get("btnBrowseOpen_Click")); // NOI18N
        btnBrowseOpen.setText(resourceMap.getString("btnBrowseOpen.text")); // NOI18N
        btnBrowseOpen.setName("btnBrowseOpen"); // NOI18N

        btnKlantenLezen.setAction(actionMap.get("btnKlantenLezen_Click")); // NOI18N
        btnKlantenLezen.setText(resourceMap.getString("btnKlantenLezen.text")); // NOI18N
        btnKlantenLezen.setEnabled(false);
        btnKlantenLezen.setName("btnKlantenLezen"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtResultaat.setColumns(20);
        txtResultaat.setRows(5);
        txtResultaat.setName("txtResultaat"); // NOI18N
        jScrollPane1.setViewportView(txtResultaat);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtOpenFile, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBrowseOpen))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                        .addComponent(btnKlantenLezen)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(btnBrowseOpen)
                    .addComponent(txtOpenFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnKlantenLezen)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSluiten, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSluiten)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Deze action wordt uitgevoerd als de gebruiker op de knop klikt. Hij laat
     * een SaveDialog zien en laat de gebruiker een bestand selecteren.
     */
    @Action
    public void btnBrowse_Click() {
        final JFileChooser fc = new JFileChooser();
        int keuze = fc.showSaveDialog(this);
        
        if (keuze == JFileChooser.APPROVE_OPTION) {
            txtSaveBestand.setText(fc.getSelectedFile().getAbsolutePath());
            btnSaveKlanten.setEnabled(true);
        }
    }
    
    /**
     * Deze method schrijft de klanten die in de lijst staan naar een bestand 
     * dat de gebruiker geselecteerd heeft.
     */
    private void WriteKlantenToFile() {
        //Exporteer klanten naar bestand
        int count = 0;
        int size = _klanten.size();
        try {
            FileOutputStream fos = new FileOutputStream(txtSaveBestand.getText());
            ObjectOutputStream out = new ObjectOutputStream(fos);

            while (count < size) {
                Klant klant = (Klant)_klanten.get(count);
                out.writeObject(klant);
                count++;
            }
            
            out.close();
            fos.close();
        } catch (IOException ex) {
            //bla fout afhandeling
            System.out.println(ex.getMessage());
        }
    }
    
    /**
     * Deze method leest de klantgegevens uit het door de gebruiker 
     * geselecteerde bestand.
     */
    private void ReadKlantenFromFile() {
        
        try {
            FileInputStream fis = new FileInputStream(txtOpenFile.getText());
            ObjectInputStream in = new ObjectInputStream(fis);
            
            try {
                while (true) {
                    Klant klant = (Klant)in.readObject();
                    String temp = klant.toString();
                    txtResultaat.setText(txtResultaat.getText() + "\r\n" + temp);
                }
            } catch (EOFException eof) {
                in.close();
                fis.close();
            } catch (ClassNotFoundException ex) {
                System.out.println(ex.getMessage());
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * Deze action schrijft de klanten naar het opgegeven bestand.
     */
    @Action
    public void btnSaveKlanten_Click() {
        btnBrowse.setEnabled(false);
        WriteKlantenToFile();
        btnBrowse.setEnabled(true);
        btnSaveKlanten.setEnabled(false);
        JOptionPane.showMessageDialog(this, "Klaar met schrijven bestand.", "Klaar", JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * Deze action sluit het scherm.
     */
    @Action
    public void btnSluiten_Click() {
        this.setVisible(false);
    }

    /**
     * Deze action wordt uitgevoerd als de gebruiker op de knop klikt. Hij laat
     * een OpenDialog zien en laat de gebruiker een bestand selecteren.
     */
    @Action
    public void btnBrowseOpen_Click() {
        final JFileChooser fc = new JFileChooser();
        int keuze = fc.showOpenDialog(this);
        
        if (keuze == JFileChooser.APPROVE_OPTION) {
            txtOpenFile.setText(fc.getSelectedFile().getAbsolutePath());
            btnKlantenLezen.setEnabled(true);
        }
    }

    /**
     * Deze action leest het geselecteerde klant bestand uit en laat de 
     * resultaten zien.
     */
    @Action
    public void btnKlantenLezen_Click() {
        btnBrowseOpen.setEnabled(false);
        ReadKlantenFromFile();
        btnBrowseOpen.setEnabled(true);
        btnKlantenLezen.setEnabled(false);
        JOptionPane.showMessageDialog(this, "Klaar met lezen bestand.", "Klaar", JOptionPane.INFORMATION_MESSAGE);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnBrowseOpen;
    private javax.swing.JButton btnKlantenLezen;
    private javax.swing.JButton btnSaveKlanten;
    private javax.swing.JButton btnSluiten;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtOpenFile;
    private javax.swing.JTextArea txtResultaat;
    private javax.swing.JTextField txtSaveBestand;
    // End of variables declaration//GEN-END:variables

}
